package com.qifan.energy.widget.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.qifan.energy.mvp.callback.IPhotoOptionCallback;
import com.qifan.energy.R;

public class SheetBottomDialog extends DialogFragment {

    private IPhotoOptionCallback callback = new IPhotoOptionCallback() {

        @Override
        public void onCamera() {

        }

        @Override
        public void onAlbum() {

        }

        @Override
        public void onCancel() {

        }
    };

    /* 对外提供 */
    public void setEventCallback(IPhotoOptionCallback callback) {
        this.callback = callback;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        lp.windowAnimations = R.style.BottomDialogAnimation;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());

        final View view = inflater.inflate(R.layout.dialog_bottom, null);
        findView(view);
        return view;
    }

    /* 设置View */
    private void findView(View view) {
        TextView cameraTv = view.findViewById(R.id.camera_tv);
        TextView albumTv = view.findViewById(R.id.album_tv);
        TextView cancelTv = view.findViewById(R.id.cancel_tv);
        cameraTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onCamera();
        });
        albumTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onAlbum();
        });
        cancelTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onCancel();
        });
    }
}
